package com.example.myapplicationwork;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class InterestSelectActivity extends AppCompatActivity {

    private Button btnBack;
    private Button btnConfirm;
    private CheckBox cbReading;
    private CheckBox cbPhotography;
    private CheckBox cbPingPong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_select);

        // 初始化视图
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);
        cbReading = findViewById(R.id.cb_reading);
        cbPhotography = findViewById(R.id.cb_photography);
        cbPingPong = findViewById(R.id.cb_pingpong);

        // 返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());

        // 确定按钮点击事件
        btnConfirm.setOnClickListener(v -> {
            // 收集选中的兴趣
            List<String> selectedInterests = new ArrayList<>();
            if (cbReading.isChecked()) {
                selectedInterests.add("阅读");
            }
            if (cbPhotography.isChecked()) {
                selectedInterests.add("摄影");
            }
            if (cbPingPong.isChecked()) {
                selectedInterests.add("乒乓球");
            }

            // 将选中的兴趣返回给上一个页面
            Intent intent = new Intent();
            intent.putExtra("interests", String.join(",", selectedInterests));
            setResult(RESULT_OK, intent);
            finish();
        });

        // 设置按钮点击反馈
        btnBack.setOnTouchListener((v, event) -> {
            if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                btnBack.setTextColor(getResources().getColor(android.R.color.darker_gray));
            } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                btnBack.setTextColor(getResources().getColor(android.R.color.black));
            }
            return false;
        });

        btnConfirm.setOnTouchListener((v, event) -> {
            if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                btnConfirm.setTextColor(getResources().getColor(android.R.color.darker_gray));
            } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                btnConfirm.setTextColor(getResources().getColor(android.R.color.black));
            }
            return false;
        });
    }
}
